﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Binding;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Controls
{
    public partial class ProductTree : DevExpress.XtraEditors.XtraUserControl
    {
        private readonly IBom _bomImpl = InstanceFactory.GetBom();
        public ProductTree()
        {
            InitializeComponent();
            if(GlobalClass.RunTimeMode)
            {
                DataBinder.BindingBomMainItem(this.gridLookUpEditProductItemNumber,false);
                LoadStaticData();
                gridLookUpEditProductItemNumber.EditValueChanged += new EventHandler(GridLookUpEditProductItemNumberEditValueChanged);
            }
        }

        private void LoadStaticData()
        {
            gridLookUpEditProductItemNumber.Properties.DataSource =
                _bomImpl.GetBomMainModelsByCondition(" CheckStatus = 1");
        }

        private void GridLookUpEditProductItemNumberEditValueChanged(object sender, EventArgs e)
        {
            var model = (TBomMainModel)this.gridLookUpEditProductItemNumber.Properties.GetRowByKeyValue(gridLookUpEditProductItemNumber.EditValue);
            if (model != null)
            {
                this.textEditProductItemName.Text = model.ProductItemName;
                LoadTreeList(model);
            }
        }

        private void LoadTreeList(TBomMainModel bomMainModel)
        {
            var list = _bomImpl.BuildProductTree(bomMainModel.ProductItemId, bomMainModel.ProductVersion);
            this.treeListMain.DataSource = list;
            this.treeListMain.ExpandAll();
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            LoadStaticData();
        }
    }
}
